package com.bw.exam.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.exam.pojo.Brand;
import com.bw.exam.pojo.response.R;
import com.bw.exam.service.BrandService;
import com.bw.exam.util.FreemarkerUtil;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: liuzhiping
 * @date: 2024/7/18 15:06
 **/
@Slf4j
@RestController
@RequestMapping("/platform/brand")
public class BrandController {


    @Resource
    BrandService brandService;

    /**
     * 列表数据展示方法
     * @param brand
     * @return
     */
    @RequestMapping("list")
    public R list(@RequestBody Brand brand){
//        while(true){
//            ResponseEntity list = brandService.list(brand);
//        }

        log.info("brand:{}", JSON.toJSONString(brand));

        LambdaQueryWrapper<Brand> queryWarrper = new LambdaQueryWrapper<Brand>();
        if(StringUtils.isNotBlank(brand.getName())){
            queryWarrper.like(Brand::getName,brand.getName());
        }
        // 做相等于的查询，全文匹配成功后返回
        if(StringUtils.isNotBlank(brand.getFirstChar())){
            queryWarrper.eq(Brand::getFirstChar,brand.getFirstChar());
        }
        Page<Brand> pagedb = new Page<Brand>(brand.getStartPage(),brand.getEndPage());



        Page<Brand> pageResult = brandService.page(pagedb, queryWarrper);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",pageResult.getRecords());
        map.put("total",pageResult.getTotal());

        return R.success(map);
    }



    /**
     * 列表数据展示方法
     * @return
     */
    @RequestMapping("listAll")
    public R list(){
        List<Brand> list = brandService.list();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",list);
        return R.success(map);
    }



    @GetMapping("findById")
    public R findById(@RequestParam("id")Long id){

        return R.success(brandService.getById(id));
    }

    /**
     * 根据id进行删除
     * @param
     * @return
     */
    @RequestMapping("delById")
    public R delById(@RequestParam("id")Long id){
        return R.success(brandService.removeById(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("delByIds")
    public R delByIds(@RequestBody Long[] ids){
        log.info("=========>ids:"+JSON.toJSONString(ids));
        return R.success(brandService.removeByIds(Arrays.asList(ids)));
    }

    @RequestMapping("update")
    public R update(@RequestBody Brand brand){
        return R.success(brandService.saveOrUpdate(brand));
    }

    @RequestMapping("insert")
    public R insert(@RequestBody Brand brand){
        return R.success(brandService.save(brand));
    }



    @RequestMapping("freemarker")
    public  R  freemarker(){


        HashMap<String,Object> map = new HashMap<>();
        List list = this.brandService.list();
        map.put("list",list);

        String uuid = UUID.randomUUID().toString();
        String outputFilePath = "/Users/liuzhiping/datafile/freemarker/"+uuid+".html";

        FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
        try {
            freemarkerUtil.createHtml(map,"brand",outputFilePath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        }
        return  R.success();
    }




    public static void main(String[] args) {
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

        //添加数据
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", 1);
        map1.put("name", "张三");
        map1.put("age", 10);
        dataList.add(map1);

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", 2);
        map2.put("name", "李四");
        map2.put("age", 40);
        dataList.add(map2);

        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("id", 1);
        map3.put("name", "张三");
        map3.put("age", 10);
        dataList.add(map3);

//        // 循环
//        dataList.stream().forEach(map -> {
//            String name = (String) map.get("name");
//            System.out.println(name);
//        });
//        // 过滤
//        List<Map<String, Object>> ageList = dataList.stream().filter(map -> {
//            return (Integer) map.get("id") == 1;
//        }).collect(Collectors.toList());
//
//        System.out.println(ageList);

        // 过滤
        List<Map<String, Object>> ageList = dataList.stream()
                .filter(map -> (Integer) map.get("id") == 1).collect(Collectors.toList());

        System.out.println(ageList);


        // 去重
        // select distict name from table
//        System.out.println(dataList);
//        List<Map<String, Object>> collect = dataList.stream().distinct().collect(Collectors.toList());
//        System.out.println(collect);

        // 转map后，只要某个或某些数据
//        List<Integer> idList = dataList.stream().map(map -> {
//            return (Integer) map.get("id");
//        }).collect(Collectors.toList());
//        System.out.println(idList);


//        List<Integer> idList = dataList.stream()
//                .map(map -> (Integer) map.get("id"))
//                .collect(Collectors.toList());
//        System.out.println(idList);
    }
}
